extends HSlider

@onready var bar: Sprite2D = $Bar

# 当水平滚动条的值发生变化时，计算精灵条的位置
func _on_value_changed(value: float) -> void:
	if not bar:
		return
	bar.position = calc_bar_position(value)

# 计算精灵条的位置
func calc_bar_position(value: float) -> Vector2:
	var bar_position:Vector2 = Vector2.ZERO
	bar_position.x = size.x * value / (max_value-min_value)
	return bar_position

# 当拖动精灵条时，计算水平滚动条的值
func _on_bar_draged(mouse_position: Vector2) -> void:
	var new_value: float = (mouse_position.x - position.x) / size.x * (max_value - min_value)
	if new_value > max_value:
		new_value = max_value
	elif new_value < min_value:
		new_value = min_value
	value = new_value
